package com.clkj.sunhelp.mvp.jobdeal.jobwaittodolist;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.adapter.list.ListAdapterForJobWaitToDo;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.bean.HelpProjKindItem;
import com.clkj.sunhelp.bean.JobWaitToDoItem;
import com.clkj.sunhelp.common.Constant;
import com.clkj.sunhelp.common.IntentKey;
import com.clkj.sunhelp.common.SharePreKey;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.mvp.jobdeal.jobwaittododetail.ActivityJobWaitToDoDetail;
import com.clkj.sunhelp.util.CommonUtil;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/28.
 */

public class ActivityJobWaitToDoList extends BaseActivity implements PullToRefreshBase.OnRefreshListener2, JobWaitToDoListContract.View {

    private PullToRefreshListView lv;
    private ListView originalLv;
    private RelativeLayout layoutEmpty;
    private TextView tvEmptytip;


    ListAdapterForJobWaitToDo mAdapter;
    List<JobWaitToDoItem> mJobList = new ArrayList<>();


    Integer mPageIndex;
    boolean canLoad;
    boolean isRefresh;


    //查询的项目类型
    HelpProjKindItem mJobKind;

    JobWaitToDoListContract.Presenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy_list);

        initData();
        assignView();
        initView();

        mPresenter = new JobWaitToDoListPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
        getOriginalList();
    }

    @Override
    public void initData() {

        mJobKind = (HelpProjKindItem) getIntent().getSerializableExtra(IntentKey.INTENT_KEY_JOB_KIND);

        /*mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());
        mJobList.add(new JobWaitToDoListItem());*/
    }

    @Override
    public void assignView() {
        lv = (PullToRefreshListView) findViewById(R.id.lv);
        originalLv = lv.getRefreshableView();
        layoutEmpty = (RelativeLayout) findViewById(R.id.layout_empty);
        tvEmptytip = (TextView) findViewById(R.id.tv_emptytip);
    }

    @Override
    public void initView() {
        initTitleWithRightTvOrIv(null, null, mJobKind.getItemName(), true, null, null);
        lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        lv.setOnRefreshListener(this);

        mAdapter = new ListAdapterForJobWaitToDo(mJobKind.getItemName(), mJobList, this);
        originalLv.setAdapter(mAdapter);
        originalLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent toPolicyDetail = new Intent(ActivityJobWaitToDoList.this, ActivityJobWaitToDoDetail.class);
                toPolicyDetail.putExtra(IntentKey.INTENT_KEY_JOB_ITEM, mJobList.get(i-1));
                startActivity(toPolicyDetail);
            }
        });
    }


    private void getOriginalList() {
        isRefresh = true;
        mPageIndex = 1;
        getJobWaitToDoList();
    }


    private void getMoreList() {
        if (canLoad) {
            mPageIndex++;
            getJobWaitToDoList();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        getOriginalList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        getMoreList();
    }

    @Override
    public void getJobWaitToDoList() {
        mPresenter.getJobWaitToDoList(mJobKind.getId(), (String) CommonUtil.getShareValue(SharePreKey.SP_KEY_USERID, ""),
                mPageIndex.toString(),
                Constant.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void onGetJobWaitToDoListSuccess(List<JobWaitToDoItem> jobList) {
        lv.onRefreshComplete();

        if (isRefresh) {
            mJobList.clear();
            isRefresh = false;
        }
        if (jobList.size() > 0) {
            if (jobList.size() >= Constant.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            mJobList.addAll(jobList);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (mJobList.size() == 0) {
                lv.setEmptyView(layoutEmpty);
            } else {
                ToastUtil.show(this, "已经获取全部数据", 2000);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetJobWaitToDoListError(String tip) {
        lv.onRefreshComplete();
        ToastUtil.showShort(this, tip);
        if (isRefresh) {
            mJobList.clear();
            lv.setEmptyView(layoutEmpty);

        }

        isRefresh = false;
        canLoad = false;

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(JobWaitToDoListContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false, null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }

    /*@Override
    public void getPolicyList() {
        mPresenter.getPolicyList(mPolicyKindItem.getItemName(), mPageIndex.toString(), Constant.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void onGetPolicyListSuccess(List<PolicyListItem> policyList) {
        lv.onRefreshComplete();

        if (isRefresh) {
            mPolicyList.clear();
            isRefresh = false;
        }
        if (policyList.size() > 0) {
            if (policyList.size() >= Constant.DEFAULT_PAGE_SIZE) {
                lv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            mPolicyList.addAll(policyList);

        } else {
            canLoad = false;
            lv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (mPolicyList.size() == 0) {
                lv.setEmptyView(layoutEmpty);
            } else {
                ToastUtil.show(this, "已经获取全部数据", 2000);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onGetPolicyListError(String tip) {
        lv.onRefreshComplete();
        ToastUtil.showShort(this, tip);
        if (isRefresh) {
            mPolicyList.clear();
            lv.setEmptyView(layoutEmpty);

        }

        isRefresh = false;
        canLoad = false;

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setPresenter(PolicyListContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }*/
    boolean isHidden = false;
    @Override
    protected void onPause() {
        super.onPause();
        isHidden = true;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (isHidden)
        {
            getOriginalList();
            isHidden = true;
        }
    }

}
